home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SPACE 2
/
SPACE - Library 2 - Volume 1.iso
/
apps
/
131
/
applic
/
phone.c
< prev
next >
Wrap
C/C++ Source or Header
|
1987-04-17
|
11KB
|
302 lines
/* Matt Leber
* DP 245
* Lab #4
* March 18, 1987
*/
#include <screen.h> /* Screen handling routines */
#include <osbind.h> /* Outputting functions for the ST */
#include <ctype.h> /* Contains the case conversion functions */
#include <stdio.h> /* Standard I/O functions and defs... */
struct element
{
char lname[ 15 ]; /* Up to fifteen characters for last name */
char fname[ 15 ]; /* Up to fifteen characters for first name */
char phone_num[ 15 ]; /* Phone number */
};
/* The following area is a global variable definition area */
int hinum = 0; /* Holds the highest number element used */
struct element list[ 100 ]; /* The master list */
/**************************************************************************
* toup( string ); - Converts all of string to upper case, no returns *
**************************************************************************/
void toup( string )
char *string; /* Holds the string to be converted */
{
while ( *string != '\0' ) /* Until end of string */
{
*string = toupper( *string ); /* Convert character to upper case */
++string; /* Go to next character */
}
}
/*************************************************************
* add( &info ); - Adds the information in info to the list *
* - Returns 1 if not successful *
* - Returns 0 if successful *
*************************************************************/
add( info )
struct element *info;
{
if ( hinum < 100 ) /* Check to see if we have any space available */
{
strcpy( list[ hinum ].lname , info->lname); /* copy into main list */
strcpy( list[ hinum ].fname , info->fname);
strcpy( list[ hinum ].phone_num , info->phone_num);
++hinum; /* Increment highest element number */
return( 0 );
}
return( 1 ); /* If not successfull, return an error */
}
/**********************************************************
* delete( ele_num ); - Deletes element number ele_num *
* Returns : 0 - Element was deleted *
* 1 - Element doesn't exist *
**********************************************************/
delete( ele_num )
int ele_num; /* Holds the number of the element to be deleted */
{
if ( ele_num >= hinum )
return( 1 ); /* Check to see if the element exists */
--hinum; /* Shorten the length of the list */
while ( ele_num < hinum )
{ /* Copy each element down the list */
strcpy( list[ ele_num ].lname , list[ ele_num + 1 ].lname );
strcpy( list[ ele_num ].fname , list[ ele_num + 1 ].fname );
strcpy( list[ ele_num ].phone_num , list[ ele_num +1 ].phone_num );
++ele_num; /* Go to next element */
}
return( 0 ); /* Return an O.K. */
}
/*************************************************************************
* get( &info ); - Returns the number of the element that matches the *
* information in info. Start = Starting element *
* Returns -1 if there is no match. Individual *
* strings that are empty will not be checked. *
*************************************************************************/
get( info , start )
struct element *info;
int start;
{
int count;
for ( count = start ; count < hinum ; ++count )
{
if ( info->fname[ 0 ] == '\0' || /* Checking the first name */
strcmp( info->fname , list[ count ].fname ) == 0)
{
if ( info->lname[ 0 ] == '\0' || /* Checking the last name */
strcmp( info->lname , list[ count ].lname ) == 0)
return( count ); /* Match was found, return the number */
}
}
return( -1 ); /* Did not find a match */
}
/*****************************************************************
* save_lst(); - Saves the list. Returns 1 if not successful *
*****************************************************************/
save_lst()
{
FILE *fp;
int status;
if (( fp = fopen( "PHONE.DAT" , "w" )) == NULL )
return( 1 ); /* Return an error if there is a problem */
status = fwrite( (char *) list , sizeof( struct element ) , hinum , fp );
fclose( fp );
if ( status < hinum || status < 1 )
return( 1 ); /* Return an error if there is a problem */
}
/*********************************************************************
* read_lst(); - This function will read the list off of the disk *
*********************************************************************/
void read_lst()
{
FILE *fp; /* Define the pointer to file buffer */
hinum = 0; /* Set beginning of the list */
if (( fp = fopen( "PHONE.DAT" , "r" )) != NULL )
{ /* Check to see if file is O.K. */
hinum = fread( (char *) list , sizeof( struct element ) , 100 , fp );
fclose( fp ); /* Close file */
}
}
/*****************************************************************************
* edit_info( ele_num ); - Edits the information in element number ele_num *
* - No returns *
*****************************************************************************/
void edit_info( ele_num )
int ele_num;
{
if ( ele_num >= 0 && ele_num < hinum )
{
char buffer[ 15 ]; /* Temporary inputing buffer */
cls();
Cconws("Present first name : "); /* Get first name */
Cconws( list[ ele_num ].fname );
Cconws("\n\rNew first name or <RETURN> to keep : ");
gets( buffer );
toup( buffer ); /* Convert it to upper case */
if ( buffer[ 0 ] != '\0' ) /* If they inputed a name, move it in */
strcpy( list[ ele_num ].fname , buffer );
Cconws("\n\rPresent last name : "); /* Get last name */
Cconws( list[ ele_num ].lname );
Cconws("\n\rNew last name or <RETURN> to keep : ");
gets( buffer );
toup( buffer ); /* Convert it to upper case */
if ( buffer[ 0 ] != '\0' ) /* If they inputed, move it in */
strcpy( list[ ele_num ].lname , buffer );
Cconws("\n\rPresent Phone number : "); /* Get the phone number */
Cconws( list[ ele_num ].phone_num );
Cconws("\n\rNew phone number or <RETURN> to keep : ");
gets( buffer );
if ( buffer[ 0 ] != '\0' )
strcpy( list[ ele_num ].phone_num , buffer );
}
}
/****************************************************************************
* usage(); - This function displays a message on the requirements of the *
* command line parameters. And then exits to DOS *
****************************************************************************/
usage()
{
char c;
Cconws("\n\rUsage : PHONE [+?-] Lastname [Firstname] [Phone Number]\n\r");
Cconws("+ - Add entry, must have All above information\n\r");
Cconws("- - Delete entry, must have Lastname and Firstname\n\r");
Cconws("? - Edit entry, must have Lastname and Firstname\n\r\n");
Cconws("If above parameters are omited, program will list phone numbers\n\r");
Cconws("of everybody with Lastname unless a specific Firstname is given\n\r\n");
Cconws("Press any key to continue...\n\r");
c = Crawcin();
exit( 1 );
}
/*******************************************************
* main() - This is the main section of the program *
*******************************************************/
main( argc , argv )
int argc;
char *argv[];
{
int status; /* Will hold return statuses */
struct element in_info; /* Temporary command line information */
for( status = 0 ; status < argc ; ++status )
toup( argv[ status ] ); /* Convert everything to upper case */
if ( argc < 2 )
usage(); /* Display usage message and exit to dos */
read_lst(); /* Read in the master list if it exists */
if ( strlen( argv[ 1 ] ) == 1 )
switch( *argv[ 1 ] ) /* Checking for +, -, or ? */
{
case '+' : if ( argc < 5 )
usage(); /* Check for valid number of parameters */
strcpy( in_info.lname , argv[ 2 ] );
strcpy( in_info.fname , argv[ 3 ] );
strcpy( in_info.phone_num , argv[ 4 ] );
/* Copy info into structure */
if ( get( &in_info , 0 ) > -1 )
{
Cconws("Name already exists! Delete or modify...\n\r");
exit( 1 );
}
add( &in_info );
exit( save_lst() );
break;
case '-' : if ( argc < 4 )
usage(); /* Check for valid number of parameters */
strcpy( in_info.lname , argv[ 2 ] );
strcpy( in_info.fname , argv[ 3 ] );
status = get( &in_info , 0 );
if ( status > -1 )
delete( status ); /* If we find a match, delete it */
exit( save_lst() ); /* Returns error code to op system */
break;
case '?' : if ( argc < 4 )
usage();
strcpy( in_info.lname , argv[ 2 ] );
strcpy( in_info.fname , argv[ 3 ] );
status = get( &in_info , 0 );
if ( status > -1 )
edit_info( status );
exit( save_lst() ); /* Returns error code to op system */
break;
default : usage();
break;
}
strcpy( in_info.lname , argv[ 1 ] );
if ( argc < 3 )
in_info.fname[ 0 ] = '\0'; /* If no first name, make it blank */
else
strcpy( in_info.fname , argv[ 2 ] ); /* Otherwise give it first name */
status = -1;
while(( status = get( &in_info , status + 1)) > -1 )
{
Cconws( "Name : ");
Cconws( list[ status ].fname );
Cconws( " " );
Cconws( list[ status ].lname );
Cconws( "\n\rPhone Number : " );
Cconws( list[ status ].phone_num );
Cconws( "\n\n\r" );
}
Cconws("Press any key to continue...\n\r");
status = Crawcin(); /* Wait for a key to be pressed */
exit(0);
}